﻿package CutLoad.components.window
{
	import CutLoad.*;
	import CutLoad.components.*;
	import CutLoad.components.container.*;
	import CutLoad.cookies.Fun;
	import CutLoad.cookies.Painter;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * 窗口 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CWindow extends Base
	{
		private var isSetKin:Boolean=false;
		//标题框的高度
		private var titleHeight:Number
		//标题显示的内容
		private var strTitle:String;		
		//背景框
		private var bak:Sprite;
		//拖动条
		private var dragBar:Sprite=null
		//容器框
		protected var content:ContainerS
		//容品信息
		private var containerInfo:RectRule
		//标题容器
		private var titleGroup:Sprite
		private var txtTitle:CTextField
		private var title_btnMin:CButton;
		private var title_btnMax:CButton;
		private var title_btnBakSize:CButton;
		private var title_btnClose:CButton;
		//初始化大小
		private var init_w:Number,init_h:Number;
		private var init_isDrag:Boolean
		//是否要拖动
		private var isDrag:Boolean=false;
		//最小化按钮
		private var btnMin:CButton;
		//保存最小化窗口数
		public static var numMinCount:Number=0
		private var isMin:Boolean=false;
		//关闭函数
		private var funClose:Function=null
		//点击窗口标题，把它的深度放在最前面时参考的子元素
		private var forDepathTopChild:Sprite;
		//按住窗口标题，拖动的目标
		private var dragTarget:Sprite;
		//
		public function CWindow()
		{
		}
		/**
		 * 
		 * @param strTitle 标题的名称
		 * @param isCloseBtn 是否显示关闭按钮
		 * @param isDrag 是否可以拖动
		 * @param isShowMinMaxBtn 是否显示最大化，最小化
		 * @param dragTarget 按住窗口标题，拖动的目标。注：这个参数主要是用于，当用窗口做为新组件一部份时，来确定要拖动的新组件
		 * @return 
		 * 
		 */		
		public function init(strTitle:String="",isCloseBtn:Boolean=true,isDrag:Boolean=true,isShowMinMaxBtn:Boolean=false,w:Number=400,h:Number=300,dragTarget:Sprite=null):CWindow{
			if(dragTarget==null){
				this.dragTarget=this;
			}else{
				this.dragTarget=dragTarget
			}
			var sysKinTem:Array=Skin.getInstance().getSkin(1)
			containerInfo=sysKinTem[5][0]
			if(!isSetKin){
				var btnMinTem:CButton=new CButton();				
				setSkin(sysKinTem);
				isSetKin=true;
			}
			init_w=w;
			init_h=h;
			init_isDrag=isDrag
			//
			this.strTitle=strTitle;
			//
			content=new ContainerS();
			content.setWH(w,h);
			content.setXY(containerInfo.startXY.x,containerInfo.startXY.y);
			titleGroup=new Sprite();
			content.setBak(0,0,{color:0xffffff,alpha:0.5,isBitmap:false})
			//标题容器内部
			//标题框
			txtTitle=new CTextField();
			txtTitle.init();
			txtTitle.text=strTitle;
			txtTitle.setTextFormat(CTextField.getTextFormat(0,sysKinTem[6][0],sysKinTem[6][1]));
			txtTitle.x=containerInfo.startXY.x;
			txtTitle.y=(containerInfo.startXY.y-txtTitle.getShowHeight())/2
			//拖动条
			this.isDrag=isDrag
			dragBar=Painter.getRect(10,containerInfo.startXY.y,0xffffff,0);		
			this.setWH(w,h);
			dragBar.addEventListener(MouseEvent.MOUSE_DOWN,drag);
			dragBar.addEventListener(MouseEvent.MOUSE_UP,dragup);
			//
			if(isShowMinMaxBtn){
				//最小化
				titleGroup.addChild(title_btnMin);
				//最大化/回复原来大小
				title_btnMax.x=title_btnMin.getShowWidth();
				title_btnMax.addEventListener(MouseEvent.CLICK,title_btnMax_fun);
				titleGroup.addChild(title_btnMax);
				title_btnBakSize.x=title_btnMax.x
				title_btnBakSize.visible=false;
				titleGroup.addChild(title_btnBakSize);
				title_btnClose.x=title_btnMax.x+title_btnMax.getShowWidth();
			}		
			titleGroup.addChild(title_btnClose);
			title_btnClose.addEventListener(MouseEvent.CLICK,closeBtnEvent);
			//关闭
			if(!isCloseBtn){				
				title_btnClose.visible=false;
			}
			//
			setTitleGroupXY()
			//
			this.addChild(bak);			
			this.addChild(txtTitle);
			if(isDrag){
				this.addChild(dragBar);
			}		
			this.addChild(titleGroup);
			this.addChild(content);		
			//
			setForDepathTopChild(this);
			return this
		}
		/**
		 * 获得容器
		 * @return 
		 * 
		 */		
		public function getContainer():ContainerS{
			return this.content;
		}
		/**
		 * 获得标题方本框
		 * @return 
		 * 
		 */		
		public function getTitleTextField():CTextField{
			return this.txtTitle
		}
		/**
		 * 设置标题文字 
		 * @param title
		 * @return 
		 * 
		 */		
		public function setTitle(title:String):CTextField{
			this.txtTitle.text=title
			return getTitleTextField();
		}
		/**
		 * 点击窗口标题，把它的深度放在最前面时参考的子元素
		 * @param child
		 * @return 
		 * 
		 */		
		public function setForDepathTopChild(child:Sprite):void{
			this.forDepathTopChild=child;
		}
		/**
		 * 获得窗口的背景,这里不包括容器
		 * @return 
		 * 
		 */		
		public function getBackImg():Sprite{
			return this.bak;
		}
		/**
		 * 设置皮肤 
		 * @param arrClasname
		 * @return 
		 * 
		 */		
		public function setSkin(arrClasname:Array):CWindow{
				this.bak=Base.getStrClassObject(arrClasname[0][0]) as Sprite;
				//
				btnMin=new CButton();
				btnMin.setSkin(arrClasname[1]);
				title_btnMin=btnMin;
				btnMin.addEventListener(MouseEvent.CLICK,btnMin_click);
				//
				var btnMax:CButton=new CButton();
				btnMax.setSkin(arrClasname[2]);		
				btnMax.init();
				title_btnMax=btnMax;
				//
				var btnBakSize:CButton=new CButton();
				btnBakSize.addEventListener(MouseEvent.CLICK,btnBakSize_click);
				btnBakSize.setSkin(arrClasname[3]);		
				btnBakSize.init();
				title_btnBakSize=btnBakSize;
				//
				var title_btnClose:CButton=new CButton();
				title_btnClose.setSkin(arrClasname[4]);	
				title_btnClose.init();
				this.title_btnClose=title_btnClose;	
				//
				return this;
				
		}
		/**
		 * 设置关闭按钮是作的额外方法,注：设置方法时，如果close按钮没有显示，会自动打开
		 * @param funClose 格式:function(自身:CWindow);
		 * 
		 */		
		public function setFunCloseBtn(funClose:Function):void{
			if(!title_btnClose.visible) title_btnClose.visible=true;
			this.funClose=funClose;
		}
		/**
		 * 是否显示关闭按钮
		 * @param isOk true显示 false 不显示
		 * @return 
		 * 
		 */		
		public function setCloseBtnVisible(isOk:Boolean):void{
			title_btnClose.visible=isOk;
		}
		/**
		 * 是否要以拖动 
		 * @param isOk
		 * 
		 */		
		public function setDrag(isOk:Boolean):void{
			this.isDrag=isOk;
			if(!isOk) this.stopDrag();
		}
		//设置标题右上角的按钮组的坐标
		private function setTitleGroupXY():void{
			titleGroup.x=this.getShowWidth()-titleGroup.width;
		}
		//替换最大化或还原窗口大小的按钮
		private function changebtnMaxOrbtnBakSize(isMaxBtn:Boolean=false):void{
			if(isMaxBtn){
				title_btnMax.visible=true;
				title_btnBakSize.visible=false;
			}else{
				title_btnMax.visible=false;
				title_btnBakSize.visible=true;
			}
		}
		//拖动处理,点击窗口.
		private function drag(e:MouseEvent):void{
			Fun.setTopDepath(forDepathTopChild);
			if(isDrag) dragTarget.startDrag();
		}
		//停止拖动
		private function dragup(e:MouseEvent):void{
			this.setLastXY(this.x,this.y);
			dragTarget.stopDrag();
		}
		//恢复
		private function btnBakSize_click(e:MouseEvent):void{
			this.setWH(init_w,init_h);
			this.resetLastXY()
			this.setDrag(init_isDrag);
			changebtnMaxOrbtnBakSize(true);
		}
		//最小化
		private function btnMin_click(e:MouseEvent):void{
			if(isMin){
				this.resetLastXY();
			}else{
				var xx:Number=CWindow.numMinCount*10;
				var yy:Number=this.stage.stageHeight-dragBar.height;
				this.setXY(xx,yy);
			}
			isMin=!isMin
		}
		//最大化
		private function title_btnMax_fun(e:MouseEvent):void{
			this.setXY(0,0);
			this.setWH(this.stage.stageWidth,this.stage.stageHeight);
			changebtnMaxOrbtnBakSize(false);
			if(init_isDrag) setDrag(false)
		}
		//点击关闭按钮
		private function closeBtnEvent(e:MouseEvent):void{
			if(funClose!=null){
				funClose(this);
			}
		}
		//重写---------------------------------------------
		override public function setAlpha(a:Number):void{
			bak.alpha=a;
			this.getContainer().setAlpha(a);
		}
		//消除
		override public function getShowWidth():Number
		{
			return bak.width;
		}
		override public function getShowHeight():Number
		{
			return bak.height;
		}
		override public function setWH(w:Number=0,h:Number=0):void{
			if(w!=0) bak.width=w;
			if(h!=0) bak.height=h;
			if(dragBar!=null){
				dragBar.width=w;
			}
			//设置容器长宽
			var temW:Number=this.getShowWidth()-containerInfo.endWH.w-containerInfo.startXY.x;
			var temH:Number=this.getShowHeight()-containerInfo.endWH.h-containerInfo.startXY.y;
			content.setWH(temW,temH);
			setTitleGroupXY();
		}
		/**
		 * 获得容器的宽 
		 * @return 
		 * 
		 */		
		public function getContainerShowWidth():Number{
			return this.getShowWidth()-containerInfo.endWH.w-containerInfo.startXY.x
		}
		/**
		 * 获得容器的高
		 * @return 
		 * 
		 */	
		public function getContainerShowHeight():Number{
			return this.getShowHeight()-containerInfo.endWH.h-containerInfo.startXY.y;
		}
		/**
		 * 设置 窗口内容区的长宽,注：当向窗口里加一个元件，这个窗口的内容区域刚好适应这个元件长宽时。可以调用这个方法。
		 * @param w
		 * @param h
		 * @return 
		 * 
		 */		
		public function setContentWH(w:Number,h:Number):void{
			var temW:Number=w+containerInfo.endWH.w+containerInfo.startXY.x;
			var temH:Number=h+containerInfo.endWH.h+containerInfo.startXY.y;
			setWH(temW,temH)
		}
	}
}